﻿/// <reference path="_namespace.js" />
/// <reference path="../components/Component.js" />
/// <reference path="AnimationStates.js"/>

/*****************************************************************************
Ajax library class definition. Kevin Robinson 2006.
Class: ajax.effects.Animation
/*****************************************************************************/


ajax.lang.addNamespace("ajax.effects");
$imports("ajax.effects.AnimationStates");
//-----------------------------------------------------------------------------
ajax.effects.Animation = function(doc, stepMillis, maxIterations)
{///<field name="status" type="ajax.effects.AnimationStates">The current state of the animation.</field>
    ajax.effects.Animation.superConstructor.call(this, doc);

    // For animation support
    this.stepMillis     = typeof(stepMillis) != "undefined" ? stepMillis : 50; // How fast?
    this.maxIterations  = typeof(maxIterations) != "undefined" ? maxIterations : 0;
    this.callback       = null; // Calback method
    this.handle         = 0;    // handle to timeout thread
    this.iterations     = 0;    // How mnany times around the loop
    this.status         = ajax.effects.AnimationStates.Stopped;
    this.loopHandler    = this.loop.bind(this); // Freeze dry our loop handler
    
    // Publish events
    this.getEvent(this.window, "onStart");
    this.getEvent(this.window, "onStop");          
}


//-----------------------------------------------------------------------------
ajax.lang.extend("ajax.effects.Animation", "ajax.components.Component"); // Inherit
//-----------------------------------------------------------------------------
ajax.effects.Animation.prototype.stepMillis = 0;
ajax.effects.Animation.prototype.maxIterations = 0;
ajax.effects.Animation.prototype.callback = null;
ajax.effects.Animation.prototype.handle  = 0;
ajax.effects.Animation.prototype.iterations = 0;
ajax.effects.Animation.prototype.status = ajax.effects.AnimationStates.Stopped;

//-----------------------------------------------------------------------------
ajax.effects.Animation.prototype.start = function()
{        
    this.iterations = 0;
    this.status     = ajax.effects.AnimationStates.Running;
    this.onStart.call(this);    
    this.loop();
}

//-----------------------------------------------------------------------------
ajax.effects.Animation.prototype.loop = function()
{
    this.callback(); 
    
    if (this.maxIterations > 0 && this.iterations++ > this.maxIterations-1)    
        this.stop();    
                
    if (this.status == ajax.effects.AnimationStates.Running)
        this.handle = setTimeout(this.loopHandler, this.stepMillis); // Use loopHandler as that has context stored.               
}

//-----------------------------------------------------------------------------
ajax.effects.Animation.prototype.stop = function()
{
    this.status = ajax.effects.AnimationStates.Stopped;
    clearTimeout(this.handle);
    this.handle = 0;
    this.onStop.call(this);
}

//-----------------------------------------------------------------------------
ajax.effects.Animation.prototype.toString = function()
{
    return "ajax.effects.Animation";
}

//-----------------------------------------------------------------------------
ajax.effects.Animation.cast = function(obj)
{///<returns type="ajax.effects.Animation"/>
    return obj;    
}
